﻿Imports Mysoft.Map.Data
Imports System.Data
Imports System.Xml
Imports System.Text
Imports System.Data.SqlClient
Imports System.IO


Partial Public Class Cwjk_PzRuleSet_EntryEdit
    Inherits Mysoft.Map.Application.Security.AppPage

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim strOperTypeGUID, strSQL As String
            Dim dtTable As DataTable = New DataTable()

            strOperTypeGUID = Request.QueryString("OperTypeGUID")
            If Not strOperTypeGUID Is Nothing AndAlso strOperTypeGUID <> "" Then
                strSQL = "SELECT  a.OperType,a.CwztGUID,a.Application,a.HierarchyCode,a.UsedScope,a.IsShowUsedScope,a.IsUsedScopeRepeat,a.IsDisable,a.AmountFiled,a.DataFilter " & _
                         " ,b.DataFilter AS ParentDataFilter,b.OperType AS ParentOperType,b.HierarchyCode as ParentCode FROM  p_CwjkOperType a" & _
                         " LEFT JOIN p_CwjkOperType b ON a.HierarchyCode LIKE b.HierarchyCode + '.%' AND a.CwztGUID = b.CwztGUID " & _
                         " WHERE a.OperTypeGUID = '" & strOperTypeGUID & "'"
                dtTable = MyDB.GetDataTable(strSQL)
                If dtTable.Rows.Count > 0 Then
                    Me.txtApplication.Text = dtTable.Rows(0).Item("Application").ToString()
                    Me.txtCwztGUID.Text = dtTable.Rows(0).Item("CwztGUID").ToString()
                    Me.txtHierarchyCode.Text = dtTable.Rows(0).Item("HierarchyCode").ToString()
                    Me.txtDataFilter.Text = dtTable.Rows(0).Item("DataFilter").ToString()
                    Me.txtIsShowUsedScope.Text = dtTable.Rows(0).Item("IsShowUsedScope").ToString()
                    Me.txtIsUsedScopeRepeat.Text = dtTable.Rows(0).Item("IsUsedScopeRepeat").ToString()
                    Me.txtOperType.Value = dtTable.Rows(0).Item("OperType").ToString()
                    Me.txtParentCode.Text = dtTable.Rows(0).Item("ParentCode").ToString()
                    Me.txtIsDisable.Text = dtTable.Rows(0).Item("IsDisable").ToString()
                    Me.txtParentDataFilter.Text = dtTable.Rows(0).Item("ParentDataFilter").ToString()
                    Me.txtAmountFiled.Text = dtTable.Rows(0).Item("AmountFiled").ToString()
                    Me.txtParentOperType.Value = dtTable.Rows(0).Item("ParentOperType").ToString()
                    Me.txtUsedScope.Value = dtTable.Rows(0).Item("UsedScope").ToString()
                    Me.txtOldOperType.Text = dtTable.Rows(0).Item("OperType").ToString()

                    BindPzEntryRule(strOperTypeGUID)
                End If
            End If
        End If
    End Sub
    Sub BindPzEntryRule(ByVal strOperTypeGUID As String)
        If strOperTypeGUID = "" Then
            Return
        End If
        Dim strSQL, strGroupName, strPzName, strPzz, strPzOrderCode As String
        Dim strBuild As StringBuilder = New StringBuilder()

        Dim i, j, k As Integer
        Dim dtGroupName As DataTable = New DataTable()
        Dim dtPzName As DataTable = New DataTable()
        Dim dtPzEntry As DataTable = New DataTable()
        Dim xmlDoc As XmlDocument = New XmlDocument()
        Dim groupNameEl, pzNameEl, pzEntryEl As XmlElement

        strBuild.Append(" SELECT DISTINCT GroupName,substring(PzOrderCode,1,1) AS  PzOrderCode FROM p_CwjkPzEntryRule WHERE IsDisable=0 AND OperTypeGUID = '" & strOperTypeGUID & "' ORDER BY substring(PzOrderCode,1,1) ; ")
        strBuild.Append(" SELECT DISTINCT GroupName,PzName,Pzz,PzOrderCode,IsAddEntry FROM p_CwjkPzEntryRule WHERE IsDisable=0 AND OperTypeGUID = '" & strOperTypeGUID & "' ORDER BY PzOrderCode ; ")
        strBuild.Append(" SELECT OperTypeGUID,PzEntryRuleGUID,Zy,ZySql,AmountFiled,YDDirection,KmSqlFilterRule,IsInversion,EntryNum,ZyText,")
        strBuild.Append("        AmountFiledCN,a.KjkmGUID,b.KjkmName,b.KjkmFullName,dbo.fn_p_GetHsTypeNameList(dbo.fn_p_GetHsTypeGUIDList(b.KjkmGUID)) as HsxmObjectText,Pzz,PzName,GroupName,PzOrderCode,IsAddEntry,0 AS EditStatus ")
        strBuild.Append(" FROM p_CwjkPzEntryRule a LEFT JOIN p_CwjkKjkm b ON a.KjkmGUID = b.KjkmGUID WHERE IsDisable=0 AND OperTypeGUID = '" & strOperTypeGUID & "' ORDER BY PzOrderCode,EntryNum")

        Dim conn As SqlConnection = New SqlConnection(MyDB.GetSqlConnectionString)
        Dim cmd As SqlCommand = New SqlCommand()
        Dim adapter As SqlDataAdapter = New SqlDataAdapter()
        Dim ds As DataSet = New DataSet()
        cmd.Connection = conn
        cmd.CommandText = strBuild.ToString()
        adapter.SelectCommand = cmd
        Try
            conn.Open()
            adapter.Fill(ds)
            adapter.Dispose()
            conn.Close()
            conn.Dispose()
        Catch ex As Exception
            If conn.State = ConnectionState.Open Then
                conn.Close()
                conn.Dispose()
                Throw New Exception("系统运行异常,请与系统管理员联系！")
            End If
        End Try
        If ds.Tables.Count <> 3 Then
            Return
        End If
        dtGroupName = ds.Tables(0)
        dtPzName = ds.Tables(1)
        dtPzEntry = ds.Tables(2)
        If dtGroupName.Rows.Count <= 0 Then
            Return
        End If

        xmlDoc.LoadXml("<OperType OperTypeGUID=""" & strOperTypeGUID & """ Application=""" & Me.txtApplication.Text & """ IsDisabled=""" & Me.txtIsDisable.Text & """></OperType>")
        For i = 0 To dtGroupName.Rows.Count - 1
            groupNameEl = xmlDoc.CreateElement("GN")
            groupNameEl.SetAttribute("GroupName", dtGroupName.Rows(i).Item("GroupName").ToString())

            For j = 0 To dtPzName.Rows.Count - 1
                If dtGroupName.Rows(i).Item("GroupName").ToString() = dtPzName.Rows(j).Item("GroupName").ToString() Then
                    Dim strKmSqlFilterRule As String = ""
                    pzNameEl = xmlDoc.CreateElement("PZ")
                    pzNameEl.SetAttribute("GroupName", dtPzName.Rows(j).Item("GroupName").ToString())
                    pzNameEl.SetAttribute("PzName", dtPzName.Rows(j).Item("PzName").ToString())
                    pzNameEl.SetAttribute("Pzz", dtPzName.Rows(j).Item("Pzz").ToString())
                    pzNameEl.SetAttribute("PzOrderCode", dtPzName.Rows(j).Item("PzOrderCode").ToString())
                    '同一张凭证下的分录SQL语句条件不一样，转账和换票存在此问题，正好转账和换票不允许新增分录，所以KmSqlFilterRule取凭证中最后一条分录的条件
                    'pzNameEl.SetAttribute("KmSqlFilterRule", dtPzName.Rows(j).Item("KmSqlFilterRule").ToString())
                    pzNameEl.SetAttribute("IsAddEntry", dtPzName.Rows(j).Item("IsAddEntry").ToString())
                    For k = 0 To dtPzEntry.Rows.Count - 1
                        If dtPzName.Rows(j).Item("GroupName").ToString() = dtPzEntry.Rows(k).Item("GroupName").ToString() AndAlso dtPzName.Rows(j).Item("PzName").ToString() = dtPzEntry.Rows(k).Item("PzName").ToString() Then
                            pzEntryEl = xmlDoc.CreateElement("PzEntry")
                            pzEntryEl.SetAttribute("PzEntryRuleGUID", dtPzEntry.Rows(k).Item("PzEntryRuleGUID").ToString())
                            pzEntryEl.SetAttribute("EntryNum", dtPzEntry.Rows(k).Item("EntryNum").ToString())
                            pzEntryEl.SetAttribute("KmSqlFilterRule", dtPzEntry.Rows(k).Item("KmSqlFilterRule").ToString())
                            pzEntryEl.SetAttribute("Zy", dtPzEntry.Rows(k).Item("Zy").ToString())
                            pzEntryEl.SetAttribute("ZySql", dtPzEntry.Rows(k).Item("ZySql").ToString())
                            pzEntryEl.SetAttribute("ZyText", dtPzEntry.Rows(k).Item("ZyText").ToString())
                            pzEntryEl.SetAttribute("KjkmGUID", dtPzEntry.Rows(k).Item("KjkmGUID").ToString())
                            pzEntryEl.SetAttribute("HsxmObjectText", dtPzEntry.Rows(k).Item("HsxmObjectText").ToString())
                            pzEntryEl.SetAttribute("KjkmName", dtPzEntry.Rows(k).Item("KjkmFullName").ToString())
                            pzEntryEl.SetAttribute("AmountFiled", dtPzEntry.Rows(k).Item("AmountFiled").ToString())
                            pzEntryEl.SetAttribute("AmountFiledCN", dtPzEntry.Rows(k).Item("AmountFiledCN").ToString())
                            pzEntryEl.SetAttribute("YDDirection", dtPzEntry.Rows(k).Item("YDDirection").ToString())
                            pzEntryEl.SetAttribute("IsInversion", dtPzEntry.Rows(k).Item("IsInversion").ToString())
                            pzEntryEl.SetAttribute("GroupName", dtPzEntry.Rows(k).Item("GroupName").ToString())
                            pzEntryEl.SetAttribute("PzName", dtPzEntry.Rows(k).Item("PzName").ToString())
                            pzEntryEl.SetAttribute("PzOrderCode", dtPzEntry.Rows(k).Item("PzOrderCode").ToString())
                            pzEntryEl.SetAttribute("Pzz", dtPzEntry.Rows(k).Item("Pzz").ToString())
                            pzEntryEl.SetAttribute("IsAddEntry", dtPzEntry.Rows(k).Item("IsAddEntry").ToString())
                            pzEntryEl.SetAttribute("EditStatus", dtPzEntry.Rows(k).Item("EditStatus").ToString())
                            pzNameEl.AppendChild(pzEntryEl)

                            strKmSqlFilterRule = dtPzEntry.Rows(k).Item("KmSqlFilterRule").ToString()
                        End If

                    Next
                    pzNameEl.SetAttribute("KmSqlFilterRule", strKmSqlFilterRule)
                    groupNameEl.AppendChild(pzNameEl)
                End If

            Next
            xmlDoc.DocumentElement.AppendChild(groupNameEl)
        Next
        If xmlDoc.HasChildNodes Then
            div_PzEntryRule.InnerHtml = TransXSLT(xmlDoc, "/PubProject/Cwjk/Cwjk_PzRuleSet_EntryEdit.xslt")
        End If

    End Sub
    Public Shared Function TransXSLT(ByRef xmlDoc As XmlDocument, ByVal xsltUrl As String) As String
        Try
            Dim xslt As Xsl.XslCompiledTransform = GetXsltDoc(xsltUrl)
            Dim output As New StringBuilder("")
            Dim writer As New StringWriter(output)
            xslt.Transform(xmlDoc, Nothing, writer)
            Return output.ToString()
        Catch ex As Exception
            Throw New Exception("系统运行异常,请与系统管理员联系！")
        End Try

    End Function

    ''' <summary>
    ''' 从缓存中获取指定URL的XSLT文件文档对象
    ''' </summary>
    ''' <param name="url">XSLT文件路径</param>
    ''' <returns>XslCompiledTransform</returns>
    ''' <remarks></remarks>
    Public Shared Function GetXsltDoc(ByVal url As String) As Xsl.XslCompiledTransform
        Try
            Dim key As String = url

            Dim xslt As Xsl.XslCompiledTransform

            xslt = Mysoft.Map.Caching.MyCache.Get(key)

            If xslt Is Nothing Then
                xslt = New Xsl.XslCompiledTransform
                Dim path As String = HttpContext.Current.Server.MapPath(url)
                xslt.Load(path)
                Mysoft.Map.Caching.MyCache.Insert(key, xslt, New System.Web.Caching.CacheDependency(path))
            End If
            Return xslt
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
End Class